package es.cazadoresmadridejos.web.decorator;

import org.displaytag.decorator.TableDecorator;

import es.cazadoresmadridejos.beans.manager.session.UserPreferences;
import es.cazadoresmadridejos.model.Advertisement;
import es.cazadoresmadridejos.util.constants.Constants;

public class AdvertisementDecorator extends TableDecorator{

	
	@Override
	public String startRow() {
		StringBuilder buffer = new StringBuilder();
		
		Advertisement advertisement = (Advertisement) getCurrentRowObject();

		UserPreferences userPreferences = (UserPreferences) getPageContext().getSession().getAttribute("scopedTarget.userPreferences");
		
		String deleteAdvertisement = "";
//		
		if(userPreferences.getPartner().getId().equals(advertisement.getPartner().getId())){
			deleteAdvertisement = " | <a href='#' onclick='deleteAdvertisement("+advertisement.getId()+")'>Borrar</a>";
		}
		
		String cost = advertisement.getAmount() == null?"Sin especificar":String.valueOf(advertisement.getAmount() + " &euro;");
		buffer
		.append("<tr ><td style='border-bottom:1px solid grey'>")
		.append("<div class='advertisement'>")
		.append("	<div class='advertisementPhoto'>");
		if(advertisement.getImage() != null){
			buffer.append("		<img src='../photos/ADVERTISEMENT/"+ advertisement.getImage() +"' alt='Anuncio' width='90px'/> ");
		}else{
			buffer.append("		<img src='./img/no_photo.jpg' alt='Anuncio' width='90px'/> ");
		}
		buffer.append("	</div>")
		.append("	<div style='height:90px;float:left;visibility:hidden'>_</div>")
		.append("	<div class='advertisementText'>")
		.append("		Fecha: <b>"+ Constants.sdf.format(advertisement.getDate()) +"</b> | Precio: "+ cost+ "  ")
		.append("		 | <a href='mailto:"+advertisement.getPartner().getEmail()+"'>Contactar</a> "+deleteAdvertisement+" <div class='break'>_blank</div> ")
		.append("		"+ advertisement.getDescription() +" ")
		.append("	</div>")
		.append("</div>")
		.append("</td></tr>");
		
		
		return buffer.toString();
	}
	
	@Override
	public String addRowClass() {
		return "hidden";
	}
}
